package org.grayrabbit.commons.model;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

/**
 * 
 * CUDTemplate CUD模板
 * 
 * @author sys53
 * @version $Id: CUDTemplate.java, v 0.1 2012-4-19 下午3:42:48 sys53 Exp $
 */
public class CUDTemplate<T extends MyBatisPojo> {

	public String insert(T obj){
		BEGIN();
		INSERT_INTO(obj.tablename());
		obj.caculationColumnList();
		VALUES(obj.returnInsertColumnsName(),obj.returnInsertColumnsDefine());
//		System.out.println(SQL());
		return SQL();
	}
	
	public String update(T obj) {  
        String idname = obj.id();  
          
        BEGIN();  
        
        UPDATE(obj.tablename());  
        obj.caculationColumnList();
        SET(obj.returnUpdateSet());  
        WHERE(obj.idColumnName() + "=#{" + idname + "}");  
          
        return SQL();  
    }
	
	public String delete(T obj) {

		String idname = obj.id();

		BEGIN();

		DELETE_FROM(obj.tablename());

		WHERE(obj.idColumnName() + "=#{" + idname + "}");

		return SQL();
	}
	
}
